package com.daybyday.day1.day3;

import sun.reflect.generics.tree.Tree;

import java.util.*;
import java.util.concurrent.Phaser;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/10/19

*/
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

 class point{
    int x;
    int y;
    point(int x,int y){
        this.x = x;
        this.y = y;
    }
 }
public class Solution {
    static int sum = 0;
    public static void main(String[] args) {
        Solution s = new Solution();
        int[][] grid = {{0,0},{2,2},{3,10},{5,2},{7,0}};
        System.out.println(s.minCostConnectPoints(grid));


    }

    public int minCostConnectPoints(int[][] points) {
       HashMap<point,Double> map = new HashMap<>();
       ArrayList<Double> list = new ArrayList<>();
        for (int i = 0; i < points.length - 1; i++) {
            for (int j = i+1; j < points.length; j++) {
                list.add((double) ((Math.abs(points[i][0] - points[j][0])) + (Math.abs(points[i][1] - points[j][1]))));
            }
        }
        Collections.sort(list);
        int res = 0;
        for (int i = 0; i < points.length - 1; i++) {
            res += list.get(i);
        }
        return res;
    }

    public int countNegatives(int[][] grid) {
        int res = 0;
        for (int i = grid.length - 1; i >= 0 ; i--) {
            for (int j = grid[0].length-1; j >= 0; j--) {
                if (grid[i][j]>0 && j >= 0){
                    break;
                }
                res ++;
            }
        }
        return res;
    }

    public List<String> wordSubsets(String[] A, String[] B) {
        return null;
    }

    public String mergeB(String[] B){
        StringBuilder sb = new StringBuilder();
        return sb.toString();
    }

    public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) {
        int i = 0;
        List<Integer> res = new ArrayList<>();
        while (i < puzzles.length){
          res.add(containsResult(words,puzzles[i])) ;
          i++;
        }
        return res;
    }
    public int containsResult(String[] words,String puzzle){
        int count = 0;
        for (String word : words) {
            boolean flag = true;
            for (Character character : removeRepeat(word)) {
                if (!puzzle.contains(character+"") || !word.contains(puzzle.charAt(0)+"")){
                    flag = false;
                }
            }
            if (flag){
                count++;
            }
            flag = true;
        }
        return count;
    }

    public Set<Character> removeRepeat(String s){
        char[] chars = s.toCharArray();
        Set<Character> set = new HashSet<>();
        StringBuilder res = new StringBuilder();
        for (char aChar : chars) {
          set.add(aChar);
        }

        return set;
    }

    public int minimumLengthEncoding(String[] words) {
        Set<String> good = new HashSet<String>(Arrays.asList(words));
        for (String word: words) {
            for (int k = 1; k < word.length(); ++k) {
                good.remove(word.substring(k));
            }
        }

        int ans = 0;
        for (String word: good) {
            ans += word.length() + 1;
        }
        return ans;
    }

    public ArrayList<String> containString(ArrayList<String> list,String s){
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).contains(s) || s.contains(list.get(i))){
                if (list.get(i).length() < s.length()){
                    list.remove(i);
                    list.add(s);
                }
            }else{
                list.add(s);
            }
        }
        return list;
    }

    public int[] twoSum(int[] nums, int target) {
        int i = 0;
        int j = nums.length - 1;
        while (i < nums.length && j >=0){
            if (nums[j] >= target){
                j --;
                continue;
            }
            if(nums[i] + nums[j] == target){
                return new int[]{nums[i],nums[j]};
            }else if (nums[i] + nums[j] > target){
                j--;
            }else{
                i++;
            }
        }
        return null;
    }

    public TreeNode recoverPreorder(String s){
        Stack<TreeNode> stack = new Stack<>();
        char[] str =  s.toCharArray();
        int i = 0;
        while (i < str.length){
            int count = 0;

            while (str[i] == '-'){
                count ++;
                i++;
            }

            String tmp = "";
            while (i<str.length && str[i] != '-'){
                tmp += str[i];
                i++;
            }
            i--;
                TreeNode node = new TreeNode(Integer.parseInt(tmp+""));
                if (stack.isEmpty()){
                    stack.push(node);
                }else{
                    while (stack.size() > count){
                        stack.pop();
                    }
                    TreeNode peek = stack.peek();
                    if (peek.left == null){
                        peek.left = node;
                    }else{
                        peek.right = node;
                    }

                    stack.push(node);
                }
                i++;

        }
        while(stack.size()>1){
            stack.pop();
        }
        return stack.pop();
    }

    public TreeNode recoverFromPreorder(String S) {
            Stack<TreeNode> stack = new Stack();
            char[] str = S.toCharArray();
            int i = 0;
            while(i<str.length){
                int index = 0;
                int curr = 0;
                while(i<str.length && str[i]=='-'){
                    index++;
                    i++;
                }
                while(i<str.length && str[i]!='-'){
                    curr = curr*10+(str[i]-'0');
                    i++;
                }
                i--;
                TreeNode node = new TreeNode(curr);
                if(stack.isEmpty()) stack.push(node);
                else{
                    while(stack.size()>index){
                        stack.pop();
                    }
                    TreeNode fanode = stack.peek();
                    if(fanode.left==null){
                        fanode.left = node;
                    }else{
                        fanode.right = node;
                    }
                    // stack.push(fanode);
                    stack.push(node);
                }
                i++;

            }
            while(stack.size()>1){
                stack.pop();
            }
            return stack.pop();
        }


    public String[] findRelativeRanks(int[] nums) {
        Map<Integer, String> map = new TreeMap<>();
        for (int num : nums) {
            map.put(num,"");
        }
        int size = map.size();
        int tmp = 0;
        for (Integer integer : map.keySet()) {
            if (size - tmp >3){
                map.put(integer,integer.toString());
                tmp ++ ;
                continue;
            }
            if (size - tmp == 3){
                map.put(integer,"Bronze Medal");
                tmp ++ ;
                continue;
            }
            if (size - tmp == 2){
                map.put(integer,"Silver Medal");
                tmp ++ ;
                continue;
            }
            if (size - tmp == 1){
                map.put(integer,"Gold Medal");
                tmp ++ ;
            }
        }

        String[] s = new String[nums.length];
        for (int i = 0; i < s.length; i++) {
            s[i] = map.get(nums[i]);
        }

        return s;
    }

    public TreeNode searchBST(TreeNode root, int val) {
        if(root.val == val){
            return root;
        }
        if (root == null){
            return null;
        }
        if (root.val > val){
            return  searchBST(root.left,val);

        }else{
           return searchBST(root.right,val);

        }



    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode tmp1 = l1;
        ListNode pre1 = new ListNode(Integer.MAX_VALUE);
        pre1.next = tmp1;
        ListNode tmp2 = l2;
        ListNode pre2 = new ListNode(Integer.MAX_VALUE);
        pre2.next = tmp2;
        ListNode pre = pre1;

        while (tmp1!=null){
            if (tmp2 == null || tmp1 == null){
                break;
            }
            if (tmp1.val < tmp2.val){
                pre1 = tmp1;
                tmp1 = tmp1.next;
            }else{
                pre2.next = tmp2.next;
                tmp2.next = null;
                pre1.next = tmp2;
                tmp2.next = tmp1;
                tmp2 = pre2.next;
                pre1 = pre1.next;
            }
        }
        if (tmp1 == null){
            pre1.next = tmp2;
            pre2.next = null;
        }else{
            tmp1.next = tmp2;
            pre2.next = null;
        }
        return pre;
    }


    public void getNumToString(TreeNode root,String s){
        if (root == null){
            return;
        }
        if (root.left == null && root.right == null){
            sum += Integer.parseInt(s + root.val);
        }
        getNumToString(root.left,s + root.val);
        getNumToString(root.right,s + root.val);
    }

    public String longestDiverseString2(int a, int b, int c) {
        String res = "";
        if (a >= b && a >= c){
            while (a -- > 0){
                res += "a";
            }
            if (b >= c){
                while (b -- > 0){
                    res += "b";
                }
                while (c -- > 0){
                    res += "c";
                }
            }else{
                while (c -- > 0){
                    res += "c";
                }
                while (b -- > 0){
                    res += "b";
                }
            }
        }else if (b > c){
            while (b-->0){
                res += "b";
            }
            if (a >= c){
                while (a-->0){
                    res += "a";
                }
                while (c-->0){
                    res += "c";
                }
            }else{
                while (c-->0){
                    res += "c";
                }
                while (a-- > 0){
                    res += "a";
                }
            }
        }else {
            while (c-->0){
                res += "c";
            }
            if(b >= a){
                while (b-- > 0){
                    res += "b";
                }
                while (a -- >0){
                    res += "a";
                }
            }
        }
        return res;
    }


    public String longestDiverseString(int a, int b, int c) {
        MyChar[] myChars = new MyChar[]{
                new MyChar('a', a),
                new MyChar('b', b),
                new MyChar('c', c),
        };
        StringBuilder sb = new StringBuilder();

        while (true) {
            Arrays.sort(myChars);
            //先放最多的, 如果上个放的2个字符串和剩余个数最多的字符相同，则放置次多的字符

            if (sb.length() >= 2 &&
                    sb.charAt(sb.length() - 1) == myChars[2].ch &&
                    sb.charAt(sb.length() - 2) == myChars[2].ch) {
                if (myChars[1].count-- > 0) {
                    sb.append(myChars[1].ch);
                } else {
                    break;
                }

            } else {
                if (myChars[2].count-- > 0) {
                    sb.append(myChars[2].ch);
                } else {
                    break;
                }
            }

        }

        return sb.toString();
    }

    private class MyChar implements Comparable {
        char ch;
        int count;

        public MyChar(char ch, int count) {
            this.ch = ch;
            this.count = count;
        }
        @Override
        public int compareTo(Object o) {
            MyChar other = (MyChar)o;
            return this.count - other.count;
        }
    }

    public int numPairsDivisibleBy60(int[] time) {
        int res = 0;
        int i = 0,j = 1;
        for (int k = 0; k < time.length - 1; k++) {
            for (int l = k + 1; l < time.length; l++) {
                if(((time[k]+time[l])%60) == 0){
                    res ++;
                }
            }
        }
        return res;
    }

    public List<Boolean> prefixesDivBy5(int[] A) {
        List<Boolean> res = new ArrayList<>();
        String s = "";
        for (int i : A) {
            s += i;
            if (Long.valueOf(s,2) % 5 == 0){
                res.add(true);
            }else{
                res.add(false);
            }
        }
        return res;
    }


}
class cahrSort implements Comparable {
    private  char c;
    private int a;
    cahrSort(char c, int a){
        this.c = c;
        this.a = a;
    }

    @Override
    public int compareTo(Object o) {
        cahrSort c1 = (cahrSort)o;
        return this.a - c1.a;
    }
}


